/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.msk.wlhy.service;


import com.msk.sys.service.CrudService;
import com.msk.wlhy.domain.HtLogisticsOrder;
import com.msk.wlhy.domain.HtLogisticsOrderDetail;
import com.msk.wlhy.mapper.HtLogisticsOrderDao;
import com.msk.wlhy.mapper.HtLogisticsOrderDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * <p>[]</p>
 * <p>作者：zkp</p>
 * <p>邮箱：zkp@xiangyou.com</p>
 * <p>创建时间： 2018-06-07 </p>
 * <p>类说明：物流订单Service </p>
 * <p>修改记录：</p>
 */
@Service
@Transactional(readOnly = true)
public class HtLogisticsOrderService extends CrudService<HtLogisticsOrderDao, HtLogisticsOrder> {

	@Autowired
	private HtLogisticsOrderDetailDao htLogisticsOrderDetailDao;
	@Autowired
	private HtCompanyCustomerService htCompanyCustomerService;
	public HtLogisticsOrder get(String id) {
		HtLogisticsOrder htLogisticsOrder = super.get(id);
		HtLogisticsOrderDetail htLogisticsOrderDetail = new HtLogisticsOrderDetail();
		if(htLogisticsOrder!=null){
			htLogisticsOrderDetail.setOrderId(htLogisticsOrder.getId());
			htLogisticsOrder.setHtLogisticsOrderDetailList(htLogisticsOrderDetailDao.findList(htLogisticsOrderDetail));
		}
		return htLogisticsOrder;
	}
	
	public List<HtLogisticsOrder> findList(HtLogisticsOrder htLogisticsOrder) {
		return super.findList(htLogisticsOrder);
	}
	
//	public Page<HtLogisticsOrder> findPage(Page<HtLogisticsOrder> page, HtLogisticsOrder htLogisticsOrder) {
//		User u = UserUtils.getUser();
//		if(u != null && u.getUserIdentity() != null && "2".equalsIgnoreCase(u.getUserIdentity())){
//			//货主只能看到自己的数据
//			htLogisticsOrder.setAttstr01(u.getId());
//		}else{
//			htLogisticsOrder.getSqlMap().put("dsf", BaseService.dataScopeFilter(htLogisticsOrder.getCurrentUser(), "o3", "u"));
//		}
//
//		return super.findPage(page, htLogisticsOrder);
//	}
	@Transactional(readOnly = false)
	public void saveData(HtLogisticsOrder htLogisticsOrder) {
		super.save(htLogisticsOrder);
	}
	@Transactional(readOnly = false)
	public void save(HtLogisticsOrder htLogisticsOrder){
		super.save(htLogisticsOrder);
		/*for (HtLogisticsOrderDetail htLogisticsOrderDetail : htLogisticsOrder.getHtLogisticsOrderDetailList()){
			if (htLogisticsOrderDetail.getId() == null){
				continue;
			}
			//校验发货人是否数据订单的托运客户
			String customer = htLogisticsOrder.getCustomer();//订单托运客户id
			String consigner = htLogisticsOrder.getConsigner();//订单明细发货联系人
			HtCompanyCustomer htCompanyCustomer = htCompanyCustomerService.get(consigner);
			if(htCompanyCustomer != null){
				if(!customer.equals(htCompanyCustomer.getCompanyId())){
					throw new HtException("货物【"+htLogisticsOrderDetail.getGoodsName()+"】对应的发货人不属于托运客户");
				}
			}else{
				throw new HtException("货物【"+htLogisticsOrderDetail.getGoodsName()+"】对应的发货人不存在");
			}
			htLogisticsOrderDetail.setGoodsType(htLogisticsOrder.getGoodsType());
			if (HtLogisticsOrderDetail.DEL_FLAG_NORMAL.equals(htLogisticsOrderDetail.getDelFlag())){
				//对子表货物名称做非空校验，当为空时不做任何处理，因为前台如果删除下标小的子表数据后台会自动补上空对象
				htLogisticsOrderDetail.setCompany(htLogisticsOrder.getCompany()); //订单归属公司
				htLogisticsOrderDetail.setOffice(htLogisticsOrder.getOffice()); //订单归属部门
				htLogisticsOrderDetail.setGoodsName(htLogisticsOrder.getGoodsName());//货物名称
				htLogisticsOrderDetail.setConsignee(htLogisticsOrder.getConsignee());
				htLogisticsOrderDetail.setConsigner(htLogisticsOrder.getConsigner());
				htLogisticsOrderDetail.setConsigneeNumber(htLogisticsOrder.getConsigneeNumber());
				htLogisticsOrderDetail.setConsignerNumber(htLogisticsOrder.getConsignerNumber());
				if (StringUtils.isBlank(htLogisticsOrderDetail.getId())){
					htLogisticsOrderDetail.setOrderId(htLogisticsOrder.getId());
					htLogisticsOrderDetail.preInsert();
					htLogisticsOrderDetail.setStatus("1");//待发运状态
					htLogisticsOrderDetailDao.insert(htLogisticsOrderDetail);
				}else{
					htLogisticsOrderDetail.preUpdate();
					htLogisticsOrderDetailDao.update(htLogisticsOrderDetail);
				}
			}else{
				htLogisticsOrderDetailDao.delete(htLogisticsOrderDetail);
			}
		}*/
	}
	@Transactional(readOnly = false)
	public void delete(HtLogisticsOrder htLogisticsOrder) {
		super.delete(htLogisticsOrder);
		HtLogisticsOrderDetail htLogisticsOrderDetail = new HtLogisticsOrderDetail();
		htLogisticsOrderDetail.setOrderId(htLogisticsOrder.getId());
		htLogisticsOrderDetailDao.delete(htLogisticsOrderDetail);
	}
	
	@Transactional(readOnly = false)
	public int deleteIds(String[] ids) {
		int result = 0;
		for (String id : ids) {
			super.delete(new HtLogisticsOrder(id));
			HtLogisticsOrderDetail htLogisticsOrderDetail = new HtLogisticsOrderDetail();
			htLogisticsOrderDetail.setOrderId(id);
			htLogisticsOrderDetailDao.delete(htLogisticsOrderDetail);
			result ++;
		}
		return result;
	}
	/**
	 * 根据订单明细表id串查找订单集合
	 * @param detailIds
	 * @return List<HtLogisticsOrder>
	 * @throws
	 */
	public List<HtLogisticsOrder> findListByDetailIds(String detailIds) {
		return dao.findListByDetailIds(detailIds);
	}
	/**
	 * 根据运输单id获取涉及到的所有订单数据，和反馈的装修信息
	 * @return List<Map<String,Object>>
	 * @throws
	 */
	public List<Map<String,Object>> findListByTransportId(String transportId) {
		return dao.findListByTransportId(transportId);
	}


	public Long getCountByGoodId(String goodId){
		return dao.getCountByGoodId(goodId);
	}
	
	/**
	 * 获取物流订单统计数据
	 * @description getCountGroupbyOffice
	 * @date 2018-7-3
	 * @author chuming
	 * @param htLogisticsOrder
	 * @return
	 */
	public List<Map<String,Object>> getCountGroupbyOffice(HtLogisticsOrder htLogisticsOrder){
		//htLogisticsOrder.getSqlMap().put("dsf", BaseService.dataScopeFilter(htLogisticsOrder.getCurrentUser(), "o3", "u"));
		return dao.getCountGroupbyOffice(htLogisticsOrder);
	}
	
	/**
	 * 获取物流订单详情未完成的条数
	 * @description findCountByOrderId
	 * @date 2018-7-9
	 * @author chuming
	 * @param orderId   物流订单Id
	 * @return
	 */
	public Long findCountByOrderId(String orderId){
		return dao.findCountByOrderId(orderId);
	}
	
	/**
	 * 插入数据
	 * @return
	 */
	@Transactional(readOnly = false)
	public int insert(HtLogisticsOrder htLogisticsOrder){
		return dao.insert(htLogisticsOrder);
	}
	
	/**
	 * 更新数据
	 * @return
	 */
	@Transactional(readOnly = false)
	public int update(HtLogisticsOrder htLogisticsOrder){
		return dao.update(htLogisticsOrder);
	}

	/**
	 * 根据Id条件更新数据
	 * @param htLogisticsOrder
	 * @return
	 */
	@Transactional(readOnly = false)
	public int updateInfoById(HtLogisticsOrder htLogisticsOrder){
		return dao.updateInfoById(htLogisticsOrder);
	}

	public String getOrderNo(){
		return dao.getOrderNo();
	}

	/**
	 * 根据货源Id更新货主合同
	 * @param htLogisticsOrder
	 * @param goodsId
	 * @return
	 */
	@Transactional(readOnly = false)
	public int updateByGoodsId(HtLogisticsOrder htLogisticsOrder,String goodsId){
		return dao.updateByGoodsId(htLogisticsOrder,goodsId);
	}

	/**
	 * 根据货源Id查询货主合同
	 * @param goodsId
	 * @return
	 */
	@Transactional(readOnly = false)
	public HtLogisticsOrder selectByGoodsId(String goodsId){
		return dao.selectByGoodsId(goodsId);
	}
}